package org.bing.engine.common.scheduler;

import java.util.Collection;
import java.util.Map;

import org.bing.engine.common.logging.Log;
import org.bing.engine.common.logging.LogFactory;


public class SimpleExpireTask implements Runnable {
	private static final Log logger = LogFactory.getLog(SimpleExpireTask.class);
	private SimpleScheduler scheduler;
	private Map<String, SimpleTaskProxy> tasks;

	public SimpleExpireTask(SimpleScheduler scheduler, Map<String, SimpleTaskProxy> tasks) {
		this.scheduler = scheduler;
		this.tasks = tasks;
	}

	public void run() {
		try {
			doRun();
		} catch (Throwable t) {
			logger.warn("Fail to run expired task handler! " + t);
		}
	}

	public void doRun() {
		if (scheduler != null && tasks != null) {
			long now = System.currentTimeMillis();
			Collection<SimpleTaskProxy> collection = tasks.values();
			for (SimpleTaskProxy proxy : collection) {
				if (now > proxy.getEndTime()) {
					scheduler.cancel(proxy.getTaskName());
				}
			}
		}
	}

}
